<template>
  <div class="goods">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="container">
      <el-card>
        <div>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-input clearable @clear="clearSearch" placeholder="请输入内容" v-model="goodSearch">
                <el-button slot="append" icon="el-icon-search" @click="searchGoods"></el-button>
              </el-input>
            </el-col>
            <el-col :span="4">
              <el-button type="primary" @click="goAddGood">添加商品</el-button>
            </el-col>
          </el-row>
        </div>
        <!-- 商品列表 -->
        <el-table :data="goodsData" style="width: 100%">
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column prop="goods_name" label="商品名称"></el-table-column>
          <el-table-column prop="goods_price" label="商品价格" width="100"></el-table-column>
          <el-table-column prop="goods_weight" label="商品重量" width="100"></el-table-column>
          <el-table-column prop="add_time" label="创建时间" width="180">
            <template v-slot="scope">{{scope.row.add_time | dateCreate}}</template>
          </el-table-column>
          <el-table-column prop="edit" label="操作" width="180">
            <template v-slot="scope">
              <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="delGoods(scope.row.goods_id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="goodsSizeChange"
          @current-change="goodsCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30,50]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalGoods"
        ></el-pagination>
      </el-card>
    </div>
  </div>
</template>

<script>
import moment from 'moment'

export default {
  data() {
    return {
      goodsData: [], // 商品列表
      goodSearch: '', // 商品关键字
      queryInfo: {
        // 获取商品列表参数
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品总天数
      totalGoods: 0,
      currentPage: 1 // 当前展示条数
    }
  },
  created() {
    this.getGoods()
  },
  methods: {
    // 获取商品列表
    async getGoods() {
      const { data: res } = await this.$axios.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        this.$message.error('获取数据失败')
        return
      }
      this.totalGoods = res.data.total
      this.goodsData = res.data.goods
    },
    // 搜索商品
    searchGoods() {
      this.queryInfo.query = this.goodSearch
      this.getGoods()
    },
    clearSearch() {
      this.queryInfo.query = ''
      this.getGoods()
    },
    // 修改展示的条数
    goodsSizeChange(size) {
      this.queryInfo.pagesize = size
      this.getGoods()
    },
    // 修改当前页数
    goodsCurrentChange(num) {
      this.queryInfo.pagenum = num
      this.getGoods()
    },
    // 添加商品
    goAddGood() {
      this.$router.push('/goods/add')
    },
    // 删除商品
    delGoods(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$axios.delete('goods/' + id)
          if (res.meta.status !== 200) return this.$message.error('删除失败')
          this.$message.success('删除成功')
          this.getGoods()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  filters: {
    dateCreate: val => {
      return moment().format('YYYY-MM-DD hh:mm:ss')
    }
  }
}
</script>

<style lang='less' scoped>
.container {
  width: 100%;
  background: #fff;
  border-radius: 5px;
  box-shadow: 2px 5px 5px #ddd;
}
</style>
